package com.web;


import javax.servlet.http.HttpServlet;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.entity.User;
import com.service.LoginService;
import com.util.Md5Help;

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/pms/index.jsp").forward(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name=request.getParameter("username");
		String password=request.getParameter("password");
		LoginService ls=new LoginService();
		String pwd=Md5Help.executeMd5(password);
		User user=ls.loginByName(name);
		
		if(user!=null&&user.getPassword().equals(pwd)){
			HttpSession session=request.getSession();
			session.setAttribute("user", user);
			
			/*String where = (String) session.getAttribute("where");
			if(where != null){
				session.removeAttribute("where");
				response.sendRedirect(where);
			} else {*/
				//request.getRequestDispatcher("/WEB-INF/pms/main.jsp").forward(request, response);
				response.sendRedirect("main.jspx");
			/*}*/
		}else{
			response.sendRedirect("login.jspx?code=10001");
		}
	}
}
